#include <iostream>
#include <vector>
using namespace std;

int max_candys(vector<int> &c, int k)
{
        int n = c.size();
        vector<vector<int>> dp(n, vector<int>(k, -1));
        dp[0][0] = 0;

        int need = 0;
        for(int i = 1; i < n; i++){
                for(int j = 0; j < k; j++){
                        dp[i][j] = dp[i - 1][j];
                        need = (k + j - c[i]%k)%k;
                        if(dp[i - 1][need] != -1){
                                dp[i][j] = max(dp[i][j], dp[i - 1][need] + c[i]);
                        }
                }
        }
        return dp.back()[0];
}

int main()
{
        int n, k;
        cin>>n>>k;
        vector<int> candy(n + 1);
        for(int i = 1; i <= n; i++){
                cin>>candy[i];
        }
        cout<<max_candys(candy, k)<<endl;
        return 0;
}